package com.example.farmer.Activity;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Adapter.PostCircleAdapter;
import com.example.farmer.Database.CirclePostDatabase;
import com.example.farmer.Entity.CirclePost;
import com.example.farmer.R;

import java.util.List;

public class MineCircleActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private TextView textViewPostContent;
    private List<CirclePost> circlePosts;
    private PostCircleAdapter adapter;
    private CirclePostDatabase circlePostDatabase;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_circle);

        textViewPostContent = findViewById(R.id.poster_not_published);

        // 初始化数据库
        circlePostDatabase = CirclePostDatabase.getInstance(this);
        circlePosts = circlePostDatabase.getAllPosts();

        // 设置布局管理器
        recyclerView = findViewById(R.id.recyclerViewPosts);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 创建适配器并设置到RecyclerView
       adapter = new PostCircleAdapter(circlePosts,this); // 假设您有一个适配器类PostAdapter
        recyclerView.setAdapter(adapter);

        updateUiWithPosts();
    }

    private void updateUiWithPosts() {
        // 如果posts不为空，则通知适配器数据已更改
        if (circlePosts != null && !circlePosts.isEmpty()) {
            // 假设PostAdapter实现了相应的方法来更新数据
            adapter = (PostCircleAdapter) recyclerView.getAdapter();
            if (adapter != null) {
                adapter.updatePosts(circlePosts);
            }
        } else {
            textViewPostContent.setText("你还没有发布海报圈哦"); // 假设您有一个TextView来显示消息
        }
    }
}